<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>Reranker Framework (ReFr): perceptron-model.C Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">Reranker Framework (ReFr)
   
   </div>
   <div id="projectbrief">Reranking framework for structure prediction and discriminative language modeling</div>
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">perceptron-model.C</div>  </div>
</div><!--header-->
<div class="contents">
<a href="perceptron-model_8_c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright 2012, Google Inc.</span>
<a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span>
<a name="l00003"></a>00003 <span class="comment">//</span>
<a name="l00004"></a>00004 <span class="comment">// Redistribution and use in source and binary forms, with or without</span>
<a name="l00005"></a>00005 <span class="comment">// modification, are permitted provided that the following conditions are</span>
<a name="l00006"></a>00006 <span class="comment">// met:</span>
<a name="l00007"></a>00007 <span class="comment">//</span>
<a name="l00008"></a>00008 <span class="comment">//   * Redistributions of source code must retain the above copyright</span>
<a name="l00009"></a>00009 <span class="comment">//     notice, this list of conditions and the following disclaimer.</span>
<a name="l00010"></a>00010 <span class="comment">//   * Redistributions in binary form must reproduce the above</span>
<a name="l00011"></a>00011 <span class="comment">//     copyright notice, this list of conditions and the following disclaimer</span>
<a name="l00012"></a>00012 <span class="comment">//     in the documentation and/or other materials provided with the</span>
<a name="l00013"></a>00013 <span class="comment">//     distribution.</span>
<a name="l00014"></a>00014 <span class="comment">//   * Neither the name of Google Inc. nor the names of its</span>
<a name="l00015"></a>00015 <span class="comment">//     contributors may be used to endorse or promote products derived from</span>
<a name="l00016"></a>00016 <span class="comment">//     this software without specific prior written permission.</span>
<a name="l00017"></a>00017 <span class="comment">//</span>
<a name="l00018"></a>00018 <span class="comment">// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span>
<a name="l00019"></a>00019 <span class="comment">// &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span>
<a name="l00020"></a>00020 <span class="comment">// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR</span>
<a name="l00021"></a>00021 <span class="comment">// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT</span>
<a name="l00022"></a>00022 <span class="comment">// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
<a name="l00023"></a>00023 <span class="comment">// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT</span>
<a name="l00024"></a>00024 <span class="comment">// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,</span>
<a name="l00025"></a>00025 <span class="comment">// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY</span>
<a name="l00026"></a>00026 <span class="comment">// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span>
<a name="l00027"></a>00027 <span class="comment">// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE</span>
<a name="l00028"></a>00028 <span class="comment">// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<a name="l00029"></a>00029 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00030"></a>00030 <span class="comment">//</span>
<a name="l00031"></a>00031 <span class="comment">//</span>
<a name="l00035"></a>00035 <span class="comment"></span>
<a name="l00036"></a><a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">00036</a> <span class="preprocessor">#define DEBUG 1</span>
<a name="l00037"></a>00037 <span class="preprocessor"></span>
<a name="l00038"></a>00038 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00039"></a>00039 <span class="preprocessor">#include &lt;vector&gt;</span>
<a name="l00040"></a>00040 <span class="preprocessor">#include &lt;unordered_set&gt;</span>
<a name="l00041"></a>00041 
<a name="l00042"></a>00042 <span class="preprocessor">#include &quot;<a class="code" href="candidate-set_8_h.html" title="Class to hold a single training instance for a reranker, which is a set of examples, typically the n-best output of some input process, posibly including a gold-standard feature vector.">candidate-set.H</a>&quot;</span>
<a name="l00043"></a>00043 <span class="preprocessor">#include &quot;<a class="code" href="training-time_8_h.html" title="Provides the reranker::Time class, which holds the three notions of training time: current epoch...">training-time.H</a>&quot;</span>
<a name="l00044"></a>00044 
<a name="l00045"></a>00045 <span class="preprocessor">#include &quot;<a class="code" href="perceptron-model_8_h.html" title="Provides the reranker::PerceptronModel reranker class.">perceptron-model.H</a>&quot;</span>
<a name="l00046"></a>00046 
<a name="l00047"></a>00047 <span class="keyword">using</span> std::cerr;
<a name="l00048"></a>00048 <span class="keyword">using</span> std::endl;
<a name="l00049"></a>00049 <span class="keyword">using</span> std::vector;
<a name="l00050"></a>00050 <span class="keyword">using</span> std::unordered_set;
<a name="l00051"></a>00051 
<a name="l00052"></a>00052 <span class="keyword">namespace </span>reranker {
<a name="l00053"></a>00053 
<a name="l00054"></a><a class="code" href="namespacereranker.html#a7ef658f24a060585d5758ffb4bea1463">00054</a> <a class="code" href="model_8_h.html#a9ebfdbfca885b4b3db9fd5936dcfa553" title="Registers the Model  implementation with the specified subtype TYPE with the Model  Factory...">REGISTER_MODEL</a>(<a class="code" href="classreranker_1_1_perceptron_model.html" title="This class implements a perceptron model reranker.">PerceptronModel</a>)
<a name="l00055"></a>00055 
<a name="l00056"></a>00056 <a class="code" href="model_8_h.html#a1191f25c550ec9317251bfc0850d6988" title="Registers the Model::UpdatePredicate  implementation with the specified subtype TYPE and NAME with th...">REGISTER_NAMED_MODEL_UPDATE_PREDICATE</a>(<a class="code" href="classreranker_1_1_perceptron_model.html" title="This class implements a perceptron model reranker.">PerceptronModel</a>::DefaultUpdatePredicate,
<a name="l00057"></a><a class="code" href="namespacereranker.html#ae13f34e1bdbbb1e59621ca8ea73bb853">00057</a>                                       PerceptronModelDefaultUpdatePredicate)
<a name="l00058"></a>00058 
<a name="l00059"></a>00059 <a class="code" href="model_8_h.html#ada2995d09a8d1bcb3745fc4cc38c8daf" title="Registers the Model::Updater  implementation with the specified subtype TYPE and NAME with the Model:...">REGISTER_NAMED_MODEL_UPDATER</a>(<a class="code" href="classreranker_1_1_perceptron_model.html" title="This class implements a perceptron model reranker.">PerceptronModel</a>::DefaultUpdater,
<a name="l00060"></a><a class="code" href="namespacereranker.html#a70583da224ccb6ae29492c458c35bb04">00060</a>                              PerceptronModelDefaultUpdater)
<a name="l00061"></a>00061 
<a name="l00062"></a>00062 <span class="keywordtype">string</span>
<a name="l00063"></a>00063 <a class="code" href="classreranker_1_1_perceptron_model.html" title="This class implements a perceptron model reranker.">PerceptronModel</a>::proto_reader_spec_(&quot;<a class="code" href="classreranker_1_1_perceptron_model_proto_reader.html" title="A class to construct a PerceptronModel from a ModelMessage instance.">PerceptronModelProtoReader</a>()&quot;);
<a name="l00064"></a>00064 
<a name="l00065"></a>00065 <span class="keywordtype">string</span>
<a name="l00066"></a>00066 <a class="code" href="classreranker_1_1_perceptron_model.html" title="This class implements a perceptron model reranker.">PerceptronModel</a>::proto_writer_spec_(&quot;<a class="code" href="classreranker_1_1_perceptron_model_proto_writer.html" title="A class to construct a ModelMessage from a PerceptronModel instance.">PerceptronModelProtoWriter</a>()&quot;);
<a name="l00067"></a>00067 
<a name="l00068"></a>00068 
<a name="l00069"></a>00069 <span class="keywordtype">void</span>
<a name="l00070"></a><a class="code" href="classreranker_1_1_perceptron_model.html#aa2ce4f882d64528ae8e8daba60a54d3f">00070</a> <a class="code" href="classreranker_1_1_perceptron_model.html" title="This class implements a perceptron model reranker.">PerceptronModel</a>::RegisterInitializers(<a class="code" href="classreranker_1_1_initializers.html" title="A container for all the member initializers for a particular Factory-constructible instance...">Initializers</a> &amp;initializers) {
<a name="l00071"></a>00071   <span class="keywordtype">bool</span> required = <span class="keyword">true</span>;
<a name="l00072"></a>00072   initializers.Add(<span class="stringliteral">&quot;name&quot;</span>, &amp;name_, required);
<a name="l00073"></a>00073   initializers.Add(<span class="stringliteral">&quot;score_comparator&quot;</span>, &amp;score_comparator_);
<a name="l00074"></a>00074   initializers.Add(<span class="stringliteral">&quot;gold_comparator&quot;</span>, &amp;gold_comparator_);
<a name="l00075"></a>00075   initializers.Add(<span class="stringliteral">&quot;candidate_set_scorer&quot;</span>, &amp;candidate_set_scorer_);
<a name="l00076"></a>00076   initializers.Add(<span class="stringliteral">&quot;update_predicate&quot;</span>, &amp;update_predicate_);
<a name="l00077"></a>00077   initializers.Add(<span class="stringliteral">&quot;updater&quot;</span>, &amp;updater_);
<a name="l00078"></a>00078   initializers.Add(<span class="stringliteral">&quot;step_size&quot;</span>, &amp;step_size_);
<a name="l00079"></a>00079 }
<a name="l00080"></a>00080 
<a name="l00081"></a>00081 <span class="keywordtype">void</span>
<a name="l00082"></a><a class="code" href="classreranker_1_1_perceptron_model.html#a2043a39df9801a012c7f834fd4c54be0">00082</a> <a class="code" href="classreranker_1_1_perceptron_model.html#a2043a39df9801a012c7f834fd4c54be0" title="Initializes this instance.">PerceptronModel::Init</a>(<span class="keyword">const</span> <a class="code" href="classreranker_1_1_environment.html" title="An interface for an environment in which variables of various types are mapped to their values...">Environment</a> *env, <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;arg) {
<a name="l00083"></a>00083   <a class="code" href="classreranker_1_1_perceptron_model.html#ae18957d720914dd4bd4528f3de5b282d">model_spec_</a>.clear();
<a name="l00084"></a>00084   <a class="code" href="classreranker_1_1_perceptron_model.html#ae18957d720914dd4bd4528f3de5b282d">model_spec_</a>.append(arg);
<a name="l00085"></a>00085 }
<a name="l00086"></a>00086 
<a name="l00087"></a>00087 <span class="keywordtype">void</span>
<a name="l00088"></a><a class="code" href="classreranker_1_1_perceptron_model.html#a8ccab23af92f9a7db985257791f55acb">00088</a> <a class="code" href="classreranker_1_1_perceptron_model.html#a8ccab23af92f9a7db985257791f55acb" title="Trains this model on a collection of training examples, where each training example is a set of candi...">PerceptronModel::Train</a>(<a class="code" href="classreranker_1_1_candidate_set_iterator.html" title="An interface specifying iteration over CandidateSet instances, using Java-style semantics (sorry...">CandidateSetIterator</a> &amp;examples,
<a name="l00089"></a>00089                        <a class="code" href="classreranker_1_1_candidate_set_iterator.html" title="An interface specifying iteration over CandidateSet instances, using Java-style semantics (sorry...">CandidateSetIterator</a> &amp;development_test) {
<a name="l00090"></a>00090   <span class="keywordflow">while</span> (<a class="code" href="classreranker_1_1_perceptron_model.html#a089a2268c88e89684c03d0730c5691c7" title="Returns whether more training epochs are required for this model.">NeedToKeepTraining</a>()) {
<a name="l00091"></a>00091     <a class="code" href="classreranker_1_1_perceptron_model.html#a598e88d61b4b35b2b5df59a00fc73d6e">NewEpoch</a>();
<a name="l00092"></a>00092     <a class="code" href="classreranker_1_1_perceptron_model.html#ae88557a6953807829c8d5fb760589487" title="Trains this model for one epoch, i.e., a single pass through the specified set of training examples...">TrainOneEpoch</a>(examples);
<a name="l00093"></a>00093     <a class="code" href="classreranker_1_1_perceptron_model.html#a615950ed57b7ea2c729b374438f8d31e">Evaluate</a>(development_test);
<a name="l00094"></a>00094     <span class="comment">// TODO(dbikel,kbhall): Iterative parameter mixing goes here.</span>
<a name="l00095"></a>00095     <span class="comment">//                      Keith: Please note that FeatureVector has</span>
<a name="l00096"></a>00096     <span class="comment">//                      an AddScaledVector method which is</span>
<a name="l00097"></a>00097     <span class="comment">//                      probably useful here.</span>
<a name="l00098"></a>00098   }
<a name="l00099"></a>00099   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a>) {
<a name="l00100"></a>00100     cerr &lt;&lt; <span class="stringliteral">&quot;Best model epoch: &quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_perceptron_model.html#acc08b37db0b4b7ea4cd8d340a4e86216" title="The epoch of the best models seen so far during training.">best_model_epoch_</a> &lt;&lt; endl;
<a name="l00101"></a>00101     cerr &lt;&lt; <span class="stringliteral">&quot;Total elpased time: &quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a07f9ec2004438a55f7813e70c50423c6">absolute_seconds</a>() &lt;&lt; <span class="stringliteral">&quot; seconds.&quot;</span>
<a name="l00102"></a>00102          &lt;&lt; endl;
<a name="l00103"></a>00103   }
<a name="l00104"></a>00104   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a> &gt;= 2) {
<a name="l00105"></a>00105     cerr &lt;&lt; <span class="stringliteral">&quot;Final raw model: &quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#a98dd763d1e1b3b980e07091488f9b422" title="Returns either the raw or averaged feature vector, depending on the argument.">GetModel</a>(<span class="keyword">true</span>) &lt;&lt; endl
<a name="l00106"></a>00106          &lt;&lt; <span class="stringliteral">&quot;Final averaged model: &quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#a98dd763d1e1b3b980e07091488f9b422" title="Returns either the raw or averaged feature vector, depending on the argument.">GetModel</a>(<span class="keyword">false</span>) &lt;&lt; endl;
<a name="l00107"></a>00107     cerr &lt;&lt; <span class="stringliteral">&quot;Final best raw model: &quot;</span>
<a name="l00108"></a>00108          &lt;&lt; <a class="code" href="classreranker_1_1_perceptron_model.html#a234283287d5a7f59fd8a1d1cf767fbc1" title="The best models seen so far during training, according to evaluation on the held-out development test...">best_models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#a98dd763d1e1b3b980e07091488f9b422" title="Returns either the raw or averaged feature vector, depending on the argument.">GetModel</a>(<span class="keyword">true</span>) &lt;&lt; endl
<a name="l00109"></a>00109          &lt;&lt; <span class="stringliteral">&quot;Final best averaged model: &quot;</span>
<a name="l00110"></a>00110          &lt;&lt; <a class="code" href="classreranker_1_1_perceptron_model.html#a234283287d5a7f59fd8a1d1cf767fbc1" title="The best models seen so far during training, according to evaluation on the held-out development test...">best_models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#a98dd763d1e1b3b980e07091488f9b422" title="Returns either the raw or averaged feature vector, depending on the argument.">GetModel</a>(<span class="keyword">false</span>) &lt;&lt; endl;
<a name="l00111"></a>00111   }
<a name="l00112"></a>00112 }
<a name="l00113"></a>00113 
<a name="l00114"></a>00114 <span class="keywordtype">bool</span>
<a name="l00115"></a><a class="code" href="classreranker_1_1_perceptron_model.html#a089a2268c88e89684c03d0730c5691c7">00115</a> <a class="code" href="classreranker_1_1_perceptron_model.html#a089a2268c88e89684c03d0730c5691c7" title="Returns whether more training epochs are required for this model.">PerceptronModel::NeedToKeepTraining</a>() {
<a name="l00116"></a>00116   <span class="keywordtype">int</span> num_epochs = <a class="code" href="classreranker_1_1_model.html#a6385b58c2b89e43f00033bbed7ead046" title="Returns the current training time of this model: number of epochs, number of time steps in the curren...">time</a>().<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>() + 1;
<a name="l00117"></a>00117 
<a name="l00118"></a>00118   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a>) {
<a name="l00119"></a>00119     <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_model.html#a02cba71a3c90cec312f0244c39ed30a5" title="Returns the maximum number of epochs to train.">max_epochs</a>() &gt; 0) {
<a name="l00120"></a>00120       <span class="keywordflow">if</span> (num_epochs &lt; <a class="code" href="classreranker_1_1_model.html#a02cba71a3c90cec312f0244c39ed30a5" title="Returns the maximum number of epochs to train.">max_epochs</a>()) {
<a name="l00121"></a>00121         cerr &lt;&lt; <span class="stringliteral">&quot;Training because we have trained only &quot;</span> &lt;&lt; num_epochs
<a name="l00122"></a>00122              &lt;&lt; <span class="stringliteral">&quot; epochs but max epochs is &quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#a02cba71a3c90cec312f0244c39ed30a5" title="Returns the maximum number of epochs to train.">max_epochs</a>() &lt;&lt; <span class="stringliteral">&quot;.&quot;</span> &lt;&lt; endl;
<a name="l00123"></a>00123       }
<a name="l00124"></a>00124       <span class="keywordflow">else</span> {
<a name="l00125"></a>00125         cerr &lt;&lt; <span class="stringliteral">&quot;Stopping training because we have trained &quot;</span>
<a name="l00126"></a>00126              &lt;&lt; num_epochs &lt;&lt; <span class="stringliteral">&quot; epochs and max epochs is &quot;</span>
<a name="l00127"></a>00127              &lt;&lt; <a class="code" href="classreranker_1_1_model.html#a02cba71a3c90cec312f0244c39ed30a5" title="Returns the maximum number of epochs to train.">max_epochs</a>() &lt;&lt; <span class="stringliteral">&quot;.&quot;</span> &lt;&lt; endl;
<a name="l00128"></a>00128       }
<a name="l00129"></a>00129     }
<a name="l00130"></a>00130   }
<a name="l00131"></a>00131 
<a name="l00132"></a>00132   <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_model.html#a02cba71a3c90cec312f0244c39ed30a5" title="Returns the maximum number of epochs to train.">max_epochs</a>() &gt; 0 &amp;&amp; num_epochs &gt;= <a class="code" href="classreranker_1_1_model.html#a02cba71a3c90cec312f0244c39ed30a5" title="Returns the maximum number of epochs to train.">max_epochs</a>()) {
<a name="l00133"></a>00133     <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00134"></a>00134   }
<a name="l00135"></a>00135 
<a name="l00136"></a>00136   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a>) {
<a name="l00137"></a>00137     <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_model.html#ae376fddf5518ab1e7a9f5b5d114afe7b" title="Returns the minimum number of epochs to train.">min_epochs</a>() &gt; 0 &amp;&amp; num_epochs &lt; <a class="code" href="classreranker_1_1_model.html#ae376fddf5518ab1e7a9f5b5d114afe7b" title="Returns the minimum number of epochs to train.">min_epochs</a>()) {
<a name="l00138"></a>00138         cerr &lt;&lt; <span class="stringliteral">&quot;Training because we have trained &quot;</span> &lt;&lt; num_epochs
<a name="l00139"></a>00139              &lt;&lt; <span class="stringliteral">&quot; epochs but min epochs is &quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ae376fddf5518ab1e7a9f5b5d114afe7b" title="Returns the minimum number of epochs to train.">min_epochs</a>() &lt;&lt; <span class="stringliteral">&quot;.&quot;</span> &lt;&lt; endl;
<a name="l00140"></a>00140     }
<a name="l00141"></a>00141   }
<a name="l00142"></a>00142 
<a name="l00143"></a>00143   <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_model.html#ae376fddf5518ab1e7a9f5b5d114afe7b" title="Returns the minimum number of epochs to train.">min_epochs</a>() &gt; 0 &amp;&amp; num_epochs &lt; <a class="code" href="classreranker_1_1_model.html#ae376fddf5518ab1e7a9f5b5d114afe7b" title="Returns the minimum number of epochs to train.">min_epochs</a>()) {
<a name="l00144"></a>00144     <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00145"></a>00145   }
<a name="l00146"></a>00146 
<a name="l00147"></a>00147   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a>) {
<a name="l00148"></a>00148     <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_perceptron_model.html#a0c25e67c7e7e5cef36bbc1b1fb1cd111" title="The current number of training epochs in which the model has been degrading in development set perfor...">num_epochs_in_decline_</a> &lt; <a class="code" href="classreranker_1_1_perceptron_model.html#ad2d89585eec1b54986043d6f29a516c3" title="The maximum number of training epochs to keep training after the model starts to degrade (i...">max_epochs_in_decline_</a>) {
<a name="l00149"></a>00149       cerr &lt;&lt; <span class="stringliteral">&quot;Training because num epochs in decline is &quot;</span>
<a name="l00150"></a>00150            &lt;&lt; <a class="code" href="classreranker_1_1_perceptron_model.html#a0c25e67c7e7e5cef36bbc1b1fb1cd111" title="The current number of training epochs in which the model has been degrading in development set perfor...">num_epochs_in_decline_</a> &lt;&lt; <span class="stringliteral">&quot; which is less than &quot;</span>
<a name="l00151"></a>00151            &lt;&lt; <a class="code" href="classreranker_1_1_perceptron_model.html#ad2d89585eec1b54986043d6f29a516c3" title="The maximum number of training epochs to keep training after the model starts to degrade (i...">max_epochs_in_decline_</a> &lt;&lt; <span class="stringliteral">&quot;.&quot;</span> &lt;&lt; endl;
<a name="l00152"></a>00152     } <span class="keywordflow">else</span> {
<a name="l00153"></a>00153       cerr &lt;&lt; <span class="stringliteral">&quot;Stopping training because num epochs in decline is &quot;</span>
<a name="l00154"></a>00154            &lt;&lt; <a class="code" href="classreranker_1_1_perceptron_model.html#a0c25e67c7e7e5cef36bbc1b1fb1cd111" title="The current number of training epochs in which the model has been degrading in development set perfor...">num_epochs_in_decline_</a> &lt;&lt; <span class="stringliteral">&quot; which is greater than or equal to &quot;</span>
<a name="l00155"></a>00155            &lt;&lt; <a class="code" href="classreranker_1_1_perceptron_model.html#ad2d89585eec1b54986043d6f29a516c3" title="The maximum number of training epochs to keep training after the model starts to degrade (i...">max_epochs_in_decline_</a> &lt;&lt; <span class="stringliteral">&quot;.&quot;</span> &lt;&lt; endl;
<a name="l00156"></a>00156     }
<a name="l00157"></a>00157   }
<a name="l00158"></a>00158 
<a name="l00159"></a>00159   <span class="keywordflow">return</span> <a class="code" href="classreranker_1_1_perceptron_model.html#a0c25e67c7e7e5cef36bbc1b1fb1cd111" title="The current number of training epochs in which the model has been degrading in development set perfor...">num_epochs_in_decline_</a> &lt; <a class="code" href="classreranker_1_1_perceptron_model.html#ad2d89585eec1b54986043d6f29a516c3" title="The maximum number of training epochs to keep training after the model starts to degrade (i...">max_epochs_in_decline_</a>;
<a name="l00160"></a>00160 }
<a name="l00161"></a>00161 
<a name="l00162"></a>00162 <span class="keywordtype">void</span>
<a name="l00163"></a><a class="code" href="classreranker_1_1_perceptron_model.html#a598e88d61b4b35b2b5df59a00fc73d6e">00163</a> <a class="code" href="classreranker_1_1_perceptron_model.html#a598e88d61b4b35b2b5df59a00fc73d6e">PerceptronModel::NewEpoch</a>() {
<a name="l00164"></a>00164   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a>) {
<a name="l00165"></a>00165     <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>() &gt; 0) {
<a name="l00166"></a>00166       cerr &lt;&lt; <span class="stringliteral">&quot;Epoch &quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>() &lt;&lt; <span class="stringliteral">&quot;: &quot;</span>
<a name="l00167"></a>00167            &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#aec0429b62beee4807a4e62c2665f5a00">seconds_since_last_epoch</a>() &lt;&lt; <span class="stringliteral">&quot; seconds.&quot;</span> &lt;&lt; endl;
<a name="l00168"></a>00168     }
<a name="l00169"></a>00169   }
<a name="l00170"></a>00170   <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#accd3f2b8a3d1ae3c7e1b1a5bee61d88d" title="Increments the epoch counter.">NewEpoch</a>();
<a name="l00171"></a>00171   <a class="code" href="classreranker_1_1_model.html#a1f40d6deeb75c42c3cf78d73e71f0914" title="The number of errors made on training examples during each epoch.">num_training_errors_per_epoch_</a>.push_back(0);
<a name="l00172"></a>00172 }
<a name="l00173"></a>00173 
<a name="l00174"></a>00174 
<a name="l00175"></a>00175 <span class="keywordtype">void</span>
<a name="l00176"></a><a class="code" href="classreranker_1_1_perceptron_model.html#ae88557a6953807829c8d5fb760589487">00176</a> <a class="code" href="classreranker_1_1_perceptron_model.html#ae88557a6953807829c8d5fb760589487" title="Trains this model for one epoch, i.e., a single pass through the specified set of training examples...">PerceptronModel::TrainOneEpoch</a>(<a class="code" href="classreranker_1_1_candidate_set_iterator.html" title="An interface specifying iteration over CandidateSet instances, using Java-style semantics (sorry...">CandidateSetIterator</a> &amp;examples) {
<a name="l00177"></a>00177   examples.<a class="code" href="classreranker_1_1_candidate_set_iterator.html#a10751b02e9aa29e8f2a1c76085b661ac" title="Resets this iterator back to the beginning of its backing collection.">Reset</a>();
<a name="l00178"></a>00178   <span class="keywordflow">while</span> (examples.<a class="code" href="classreranker_1_1_candidate_set_iterator.html#a82457e09d587f9e83ef3db066e8ac485" title="Returns whether this iterator contains another CandidateSet.">HasNext</a>()) {
<a name="l00179"></a>00179     <a class="code" href="classreranker_1_1_perceptron_model.html#abfb9261016f61a8f660e699ccbdafa6a" title="Trains this model on the specified training example.">TrainOnExample</a>(examples.<a class="code" href="classreranker_1_1_candidate_set_iterator.html#aa318e5cebc531e88bce5f6d9077f41ee" title="Returns the next CandidateSet.">Next</a>());
<a name="l00180"></a>00180   }
<a name="l00181"></a>00181   <a class="code" href="classreranker_1_1_perceptron_model.html#a5e1359da40c7d987f2a14b7b47765a83">EndOfEpoch</a>();
<a name="l00182"></a>00182 }
<a name="l00183"></a>00183 
<a name="l00184"></a>00184 <span class="keywordtype">void</span>
<a name="l00185"></a><a class="code" href="classreranker_1_1_perceptron_model.html#a5e1359da40c7d987f2a14b7b47765a83">00185</a> <a class="code" href="classreranker_1_1_perceptron_model.html#a5e1359da40c7d987f2a14b7b47765a83">PerceptronModel::EndOfEpoch</a>() {
<a name="l00186"></a>00186   <a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#aaf0c6eb3e55fe11773d23589d98187d5">UpdateAllFeatureAverages</a>(<a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>);
<a name="l00187"></a>00187   <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_model.html#a97e6523258a589e4fedb67b3a6364cea" title="A hook to be performed at the end of every epoch.">end_of_epoch_hook_</a> != NULL) {
<a name="l00188"></a>00188     <a class="code" href="classreranker_1_1_model.html#a97e6523258a589e4fedb67b3a6364cea" title="A hook to be performed at the end of every epoch.">end_of_epoch_hook_</a>-&gt;<a class="code" href="classreranker_1_1_model_1_1_hook.html#a676b9a02709b0a61856a8ff246f182d6" title="The function to be executed by the Model that wraps this hook.">Do</a>(<span class="keyword">this</span>);
<a name="l00189"></a>00189   }
<a name="l00190"></a>00190   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a>) {
<a name="l00191"></a>00191     <span class="keywordtype">int</span> num_training_errors_this_epoch =
<a name="l00192"></a>00192         *<a class="code" href="classreranker_1_1_model.html#a1f40d6deeb75c42c3cf78d73e71f0914" title="The number of errors made on training examples during each epoch.">num_training_errors_per_epoch_</a>.rbegin();
<a name="l00193"></a>00193     <span class="keywordtype">double</span> percent_training_errors_this_epoch =
<a name="l00194"></a>00194         ((double)num_training_errors_this_epoch / <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a82f423a8daf71228e33b91186964f5c3" title="Returns the index of the current training example within the current epoch.">index</a>()) * 100.0;
<a name="l00195"></a>00195     cerr &lt;&lt; <span class="stringliteral">&quot;Epoch &quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>() &lt;&lt; <span class="stringliteral">&quot;: number of training errors: &quot;</span>
<a name="l00196"></a>00196          &lt;&lt; num_training_errors_this_epoch &lt;&lt; <span class="stringliteral">&quot; (&quot;</span>
<a name="l00197"></a>00197          &lt;&lt; percent_training_errors_this_epoch &lt;&lt; <span class="stringliteral">&quot;%)&quot;</span> &lt;&lt; endl;
<a name="l00198"></a>00198   }
<a name="l00199"></a>00199 }
<a name="l00200"></a>00200 
<a name="l00201"></a>00201 
<a name="l00202"></a>00202 <span class="keywordtype">void</span>
<a name="l00203"></a><a class="code" href="classreranker_1_1_perceptron_model.html#abfb9261016f61a8f660e699ccbdafa6a">00203</a> <a class="code" href="classreranker_1_1_perceptron_model.html#abfb9261016f61a8f660e699ccbdafa6a" title="Trains this model on the specified training example.">PerceptronModel::TrainOnExample</a>(<a class="code" href="classreranker_1_1_candidate_set.html" title="A class to hold a set of candidates, either for training or test.">CandidateSet</a> &amp;example) {
<a name="l00204"></a>00204   <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#aacb31465040806ff8705e168056b01aa" title="Increments both the time index for the current epoch and the absolute time index.">Tick</a>();
<a name="l00205"></a>00205 
<a name="l00206"></a>00206   <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_model.html#a8c0f904234605692035ba179f85c53e2" title="The symbol table for this model (may be NULL).">symbols_</a> != NULL) {
<a name="l00207"></a>00207     example.<a class="code" href="classreranker_1_1_candidate_set.html#a844b1e8edd6a75de2ffc5b295d558e36" title="Compiles any symbolic features in this candidate set.">CompileFeatures</a>(<a class="code" href="classreranker_1_1_model.html#a8c0f904234605692035ba179f85c53e2" title="The symbol table for this model (may be NULL).">symbols_</a>);
<a name="l00208"></a>00208   }
<a name="l00209"></a>00209 
<a name="l00210"></a>00210   <span class="keywordtype">bool</span> training = <span class="keyword">true</span>;
<a name="l00211"></a>00211   <a class="code" href="classreranker_1_1_perceptron_model.html#ace55bad139573080e682d5a269760673" title="Scores the specified set of candidates according to either the raw or averaged version of this percep...">ScoreCandidates</a>(example, training);
<a name="l00212"></a>00212 
<a name="l00213"></a>00213   <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_perceptron_model.html#a2d1e5450a8d1ccc781b9b135b7504a3f" title="Indicates whether the current model needs to be updated; the implementation here simply returns true ...">NeedToUpdate</a>(example)) {
<a name="l00214"></a>00214     <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a> &gt;= 2) {
<a name="l00215"></a>00215       cerr &lt;&lt; <span class="stringliteral">&quot;Time:&quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a1e8b9be0b48cd8cbb360f23a638e12c6">to_string</a>() &lt;&lt; <span class="stringliteral">&quot;: need to update because &quot;</span>
<a name="l00216"></a>00216            &lt;&lt; <span class="stringliteral">&quot;best scoring index &quot;</span> &lt;&lt; example.<a class="code" href="classreranker_1_1_candidate_set.html#a6052873186168c21186705c3e016d223">best_scoring_index</a>()
<a name="l00217"></a>00217            &lt;&lt; <span class="stringliteral">&quot; is not equal to gold index &quot;</span> &lt;&lt; example.<a class="code" href="classreranker_1_1_candidate_set.html#aec1af01230233ff19c30174a93a1e1b4">gold_index</a>() &lt;&lt; endl;
<a name="l00218"></a>00218     }
<a name="l00219"></a>00219     ++(*<a class="code" href="classreranker_1_1_model.html#a1f40d6deeb75c42c3cf78d73e71f0914" title="The number of errors made on training examples during each epoch.">num_training_errors_per_epoch_</a>.rbegin());
<a name="l00220"></a>00220     ++<a class="code" href="classreranker_1_1_model.html#ac9bd9064006d70c36391818fa6a628b7" title="The number of errors made on training examples.">num_training_errors_</a>;
<a name="l00221"></a>00221     <a class="code" href="classreranker_1_1_perceptron_model.html#ae7e3982866cc3db145c0e3ff4b6ef783" title="Updates the current model based on the specified set of candidates.">Update</a>(example);
<a name="l00222"></a>00222   }
<a name="l00223"></a>00223 }
<a name="l00224"></a>00224 
<a name="l00225"></a>00225 <span class="keywordtype">bool</span>
<a name="l00226"></a><a class="code" href="classreranker_1_1_perceptron_model.html#a2d1e5450a8d1ccc781b9b135b7504a3f">00226</a> <a class="code" href="classreranker_1_1_perceptron_model.html#a2d1e5450a8d1ccc781b9b135b7504a3f" title="Indicates whether the current model needs to be updated; the implementation here simply returns true ...">PerceptronModel::NeedToUpdate</a>(<a class="code" href="classreranker_1_1_candidate_set.html" title="A class to hold a set of candidates, either for training or test.">CandidateSet</a> &amp;example) {
<a name="l00227"></a>00227   <span class="keywordflow">return</span> <a class="code" href="classreranker_1_1_model.html#aac9c10b6d6a3eec5f781cfbf64bcdf22" title="The update predicate for this model.">update_predicate_</a>-&gt;NeedToUpdate(<span class="keyword">this</span>, example);
<a name="l00228"></a>00228 }
<a name="l00229"></a>00229 
<a name="l00230"></a>00230 <span class="keywordtype">bool</span>
<a name="l00231"></a>00231 PerceptronModel::DefaultUpdatePredicate::NeedToUpdate(<a class="code" href="classreranker_1_1_model.html" title="Model is an interface for reranking models.">Model</a> *model,
<a name="l00232"></a>00232                                                       <a class="code" href="classreranker_1_1_candidate_set.html" title="A class to hold a set of candidates, either for training or test.">CandidateSet</a> &amp;example) {
<a name="l00233"></a>00233   <span class="keywordflow">return</span> example.<a class="code" href="classreranker_1_1_candidate_set.html#a6052873186168c21186705c3e016d223">best_scoring_index</a>() != example.<a class="code" href="classreranker_1_1_candidate_set.html#aec1af01230233ff19c30174a93a1e1b4">gold_index</a>();
<a name="l00234"></a>00234 }
<a name="l00235"></a>00235 
<a name="l00236"></a>00236 <span class="keywordtype">void</span>
<a name="l00237"></a><a class="code" href="classreranker_1_1_perceptron_model.html#ae7e3982866cc3db145c0e3ff4b6ef783">00237</a> <a class="code" href="classreranker_1_1_perceptron_model.html#ae7e3982866cc3db145c0e3ff4b6ef783" title="Updates the current model based on the specified set of candidates.">PerceptronModel::Update</a>(<a class="code" href="classreranker_1_1_candidate_set.html" title="A class to hold a set of candidates, either for training or test.">CandidateSet</a> &amp;example) {
<a name="l00238"></a>00238   <a class="code" href="classreranker_1_1_model.html#a6af75a0de0ab2cfb3435919977341550" title="The updater for this model.">updater_</a>-&gt;Update(<span class="keyword">this</span>, example);
<a name="l00239"></a>00239 }
<a name="l00240"></a>00240 
<a name="l00241"></a>00241 <span class="keywordtype">void</span>
<a name="l00242"></a>00242 <a class="code" href="classreranker_1_1_perceptron_model.html#ae7e3982866cc3db145c0e3ff4b6ef783" title="Updates the current model based on the specified set of candidates.">PerceptronModel::DefaultUpdater::Update</a>(<a class="code" href="classreranker_1_1_model.html" title="Model is an interface for reranking models.">Model</a> *m, <a class="code" href="classreranker_1_1_candidate_set.html" title="A class to hold a set of candidates, either for training or test.">CandidateSet</a> &amp;example) {
<a name="l00243"></a>00243   <a class="code" href="classreranker_1_1_perceptron_model.html" title="This class implements a perceptron model reranker.">PerceptronModel</a> *model = <span class="keyword">dynamic_cast&lt;</span><a class="code" href="classreranker_1_1_perceptron_model.html" title="This class implements a perceptron model reranker.">PerceptronModel</a> *<span class="keyword">&gt;</span>(m);
<a name="l00244"></a>00244   ++(model-&gt;<a class="code" href="classreranker_1_1_model.html#ace86345c7febd6401dc3f48ad555f02e" title="The number of times an update was performed on this model during training.">num_updates_</a>);
<a name="l00245"></a>00245   unordered_set&lt;int&gt; gold_features;
<a name="l00246"></a>00246   unordered_set&lt;int&gt; best_scoring_features;
<a name="l00247"></a>00247   model-&gt;<a class="code" href="classreranker_1_1_perceptron_model.html#af7b4bb2f10d16bcc6bdf2428e6c09af3" title="Computes the features to be updated for the gold candidate and the best-scoring candidate.">ComputeFeaturesToUpdate</a>(example, gold_features, best_scoring_features);
<a name="l00248"></a>00248 
<a name="l00249"></a>00249   model-&gt;<a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#a157d68898da5d72f39d36c928c152693" title="Updates the feature averages the specified pair of feature uid collections, one for a gold reference ...">UpdateGoldAndCandidateFeatureAverages</a>(model-&gt;<a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>,
<a name="l00250"></a>00250                                                        gold_features,
<a name="l00251"></a>00251                                                        best_scoring_features);
<a name="l00252"></a>00252   <span class="keywordtype">double</span> step_size =
<a name="l00253"></a>00253       model-&gt;<a class="code" href="classreranker_1_1_perceptron_model.html#a64bdfab86dd1ea94c4918d409e2cd5d9" title="Computes the step size for the next update, and, as a side effect, caches this value in step_size_...">ComputeStepSize</a>(gold_features, best_scoring_features, example);
<a name="l00254"></a>00254 
<a name="l00255"></a>00255   <span class="comment">// Finally, update perceptrons.</span>
<a name="l00256"></a>00256 
<a name="l00257"></a>00257   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a> &gt;= 2) {
<a name="l00258"></a>00258     cerr &lt;&lt; <span class="stringliteral">&quot;Updating weights for gold features [&quot;</span>;
<a name="l00259"></a>00259     <span class="keywordflow">for</span> (unordered_set&lt;int&gt;::const_iterator it = gold_features.begin();
<a name="l00260"></a>00260          it != gold_features.end(); ++it) {
<a name="l00261"></a>00261       cerr &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; *it;
<a name="l00262"></a>00262     }
<a name="l00263"></a>00263     cerr &lt;&lt; <span class="stringliteral">&quot;] from\n\t&quot;</span> &lt;&lt; example.<a class="code" href="classreranker_1_1_candidate_set.html#a30da3565fca2f14edafafce0255a35ea">GetGold</a>() &lt;&lt; endl;
<a name="l00264"></a>00264 
<a name="l00265"></a>00265     cerr &lt;&lt; <span class="stringliteral">&quot;Updating weights for best scoring features [&quot;</span>;
<a name="l00266"></a>00266     <span class="keywordflow">for</span> (unordered_set&lt;int&gt;::const_iterator it = best_scoring_features.begin();
<a name="l00267"></a>00267          it != best_scoring_features.end(); ++it) {
<a name="l00268"></a>00268       cerr &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; *it;
<a name="l00269"></a>00269     }
<a name="l00270"></a>00270     cerr &lt;&lt; <span class="stringliteral">&quot;] from\n\t&quot;</span> &lt;&lt; example.<a class="code" href="classreranker_1_1_candidate_set.html#ae2d8533d6d2d3cd10a2c23a8660eefc3">GetBestScoring</a>() &lt;&lt; endl;
<a name="l00271"></a>00271 
<a name="l00272"></a>00272   }
<a name="l00273"></a>00273 
<a name="l00274"></a>00274   <span class="keywordtype">double</span> positive_step = step_size;
<a name="l00275"></a>00275   model-&gt;<a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#a362f24bcf6aac291c7e6e74a38815df9" title="Increments the weights for the specified collection of features.">UpdateWeights</a>(model-&gt;<a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>, gold_features,
<a name="l00276"></a>00276                                example.<a class="code" href="classreranker_1_1_candidate_set.html#a30da3565fca2f14edafafce0255a35ea">GetGold</a>().<a class="code" href="classreranker_1_1_candidate.html#a741433c66a4cc4dfbb1e4b89c9c6e431" title="Returns the feature vector for this candidate.">features</a>(), positive_step);
<a name="l00277"></a>00277   <span class="keywordtype">double</span> negative_step = -step_size;
<a name="l00278"></a>00278   model-&gt;<a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#a362f24bcf6aac291c7e6e74a38815df9" title="Increments the weights for the specified collection of features.">UpdateWeights</a>(model-&gt;<a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>, best_scoring_features,
<a name="l00279"></a>00279                         example.<a class="code" href="classreranker_1_1_candidate_set.html#ae2d8533d6d2d3cd10a2c23a8660eefc3">GetBestScoring</a>().<a class="code" href="classreranker_1_1_candidate.html#a741433c66a4cc4dfbb1e4b89c9c6e431" title="Returns the feature vector for this candidate.">features</a>(), negative_step);
<a name="l00280"></a>00280 
<a name="l00281"></a>00281   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a> &gt;=2) {
<a name="l00282"></a>00282     cerr &lt;&lt; <span class="stringliteral">&quot;Raw model: &quot;</span> &lt;&lt; model-&gt;<a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#a98dd763d1e1b3b980e07091488f9b422" title="Returns either the raw or averaged feature vector, depending on the argument.">GetModel</a>(<span class="keyword">true</span>) &lt;&lt; endl;
<a name="l00283"></a>00283     cerr &lt;&lt; <span class="stringliteral">&quot;Avg model: &quot;</span> &lt;&lt; model-&gt;<a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#a98dd763d1e1b3b980e07091488f9b422" title="Returns either the raw or averaged feature vector, depending on the argument.">GetModel</a>(<span class="keyword">false</span>) &lt;&lt; endl;
<a name="l00284"></a>00284   }
<a name="l00285"></a>00285 }
<a name="l00286"></a>00286 
<a name="l00287"></a>00287 <span class="keywordtype">double</span>
<a name="l00288"></a><a class="code" href="classreranker_1_1_perceptron_model.html#a615950ed57b7ea2c729b374438f8d31e">00288</a> <a class="code" href="classreranker_1_1_perceptron_model.html#a615950ed57b7ea2c729b374438f8d31e">PerceptronModel::Evaluate</a>(<a class="code" href="classreranker_1_1_candidate_set_iterator.html" title="An interface specifying iteration over CandidateSet instances, using Java-style semantics (sorry...">CandidateSetIterator</a> &amp;development_test) {
<a name="l00289"></a>00289   <span class="keywordtype">double</span> total_weight = 0.0;
<a name="l00290"></a>00290   <span class="keywordtype">double</span> total_weighted_loss = 0.0;
<a name="l00291"></a>00291   <span class="keywordtype">double</span> total_oracle_loss = 0.0;
<a name="l00292"></a>00292   <span class="keywordtype">double</span> total_baseline_loss = 0.0;
<a name="l00293"></a>00293   <a class="code" href="classreranker_1_1_model.html#ab308fe9304485e8c838ed2a196bb585b" title="The number of testing errors made on held-out development test data for each epoch.">num_testing_errors_per_epoch_</a>.push_back(0);
<a name="l00294"></a>00294 
<a name="l00295"></a>00295   <span class="keywordtype">bool</span> not_training = <span class="keyword">false</span>;
<a name="l00296"></a>00296   <span class="keywordtype">size_t</span> development_test_size = 0;
<a name="l00297"></a>00297   development_test.<a class="code" href="classreranker_1_1_candidate_set_iterator.html#a10751b02e9aa29e8f2a1c76085b661ac" title="Resets this iterator back to the beginning of its backing collection.">Reset</a>();
<a name="l00298"></a>00298   <span class="keywordflow">while</span> (development_test.<a class="code" href="classreranker_1_1_candidate_set_iterator.html#a82457e09d587f9e83ef3db066e8ac485" title="Returns whether this iterator contains another CandidateSet.">HasNext</a>()) {
<a name="l00299"></a>00299     ++development_test_size;
<a name="l00300"></a>00300     <a class="code" href="classreranker_1_1_candidate_set.html" title="A class to hold a set of candidates, either for training or test.">CandidateSet</a> &amp;candidate_set = development_test.<a class="code" href="classreranker_1_1_candidate_set_iterator.html#aa318e5cebc531e88bce5f6d9077f41ee" title="Returns the next CandidateSet.">Next</a>();
<a name="l00301"></a>00301     <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_model.html#a8c0f904234605692035ba179f85c53e2" title="The symbol table for this model (may be NULL).">symbols_</a> != NULL) {
<a name="l00302"></a>00302       candidate_set.<a class="code" href="classreranker_1_1_candidate_set.html#a844b1e8edd6a75de2ffc5b295d558e36" title="Compiles any symbolic features in this candidate set.">CompileFeatures</a>(<a class="code" href="classreranker_1_1_model.html#a8c0f904234605692035ba179f85c53e2" title="The symbol table for this model (may be NULL).">symbols_</a>);
<a name="l00303"></a>00303     }
<a name="l00304"></a>00304     <a class="code" href="classreranker_1_1_perceptron_model.html#ace55bad139573080e682d5a269760673" title="Scores the specified set of candidates according to either the raw or averaged version of this percep...">ScoreCandidates</a>(candidate_set, not_training);
<a name="l00305"></a>00305     <span class="keywordtype">double</span> loss_weight =
<a name="l00306"></a>00306         <a class="code" href="classreranker_1_1_model.html#a013aa52eb8693d1133a0d32740dffa5a">use_weighted_loss</a>() ? candidate_set.<a class="code" href="classreranker_1_1_candidate_set.html#a9bd2e0995871ec942b8117dc01293631" title="Returns the weight of the loss for this candidate set&rsquo;s reference.">loss_weight</a>() : 1.0;
<a name="l00307"></a>00307     total_weight += loss_weight;
<a name="l00308"></a>00308     total_weighted_loss += loss_weight * candidate_set.<a class="code" href="classreranker_1_1_candidate_set.html#ae2d8533d6d2d3cd10a2c23a8660eefc3">GetBestScoring</a>().<a class="code" href="classreranker_1_1_candidate.html#a024f88c11b795a31cb8f5d1b47150701" title="Returns the loss of this candidate.">loss</a>();
<a name="l00309"></a>00309     total_oracle_loss += loss_weight * candidate_set.<a class="code" href="classreranker_1_1_candidate_set.html#a30da3565fca2f14edafafce0255a35ea">GetGold</a>().<a class="code" href="classreranker_1_1_candidate.html#a024f88c11b795a31cb8f5d1b47150701" title="Returns the loss of this candidate.">loss</a>();
<a name="l00310"></a>00310 
<a name="l00311"></a>00311     <span class="comment">// For now, assume that the candidate sets are sorted by the baseline score.</span>
<a name="l00312"></a>00312     total_baseline_loss += loss_weight * candidate_set.<a class="code" href="classreranker_1_1_candidate_set.html#ae64208b8b3a7e0b19f3be10e779e2630">Get</a>(0).<a class="code" href="classreranker_1_1_candidate.html#a024f88c11b795a31cb8f5d1b47150701" title="Returns the loss of this candidate.">loss</a>();
<a name="l00313"></a>00313     <span class="keywordflow">if</span> (candidate_set.<a class="code" href="classreranker_1_1_candidate_set.html#a6052873186168c21186705c3e016d223">best_scoring_index</a>() != candidate_set.<a class="code" href="classreranker_1_1_candidate_set.html#aec1af01230233ff19c30174a93a1e1b4">gold_index</a>()) {
<a name="l00314"></a>00314       ++(*<a class="code" href="classreranker_1_1_model.html#ab308fe9304485e8c838ed2a196bb585b" title="The number of testing errors made on held-out development test data for each epoch.">num_testing_errors_per_epoch_</a>.rbegin());
<a name="l00315"></a>00315     }
<a name="l00316"></a>00316   }
<a name="l00317"></a>00317 
<a name="l00318"></a>00318   <span class="keywordtype">double</span> loss_this_epoch = total_weighted_loss / total_weight;
<a name="l00319"></a>00319   <a class="code" href="classreranker_1_1_model.html#ad3a296860f768bee866213cc0c8bbf83" title="The average loss per epoch.">loss_per_epoch_</a>.push_back(loss_this_epoch);
<a name="l00320"></a>00320 
<a name="l00321"></a>00321   <span class="keywordtype">int</span> num_testing_errors_this_epoch =
<a name="l00322"></a>00322       <a class="code" href="classreranker_1_1_model.html#ab308fe9304485e8c838ed2a196bb585b" title="The number of testing errors made on held-out development test data for each epoch.">num_testing_errors_per_epoch_</a>[<a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>()];
<a name="l00323"></a>00323   <span class="keywordtype">double</span> percent_testing_errors_this_epoch =
<a name="l00324"></a>00324       ((double)num_testing_errors_this_epoch / development_test_size) * 100.0;
<a name="l00325"></a>00325   <span class="keywordtype">double</span> oracle_loss = total_oracle_loss / total_weight;
<a name="l00326"></a>00326   <span class="keywordtype">double</span> baseline_loss = total_baseline_loss / total_weight;
<a name="l00327"></a>00327   cerr &lt;&lt; <span class="stringliteral">&quot;Epoch &quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>() &lt;&lt; <span class="stringliteral">&quot;: oracle loss: &quot;</span> &lt;&lt; oracle_loss &lt;&lt; endl;
<a name="l00328"></a>00328   cerr &lt;&lt; <span class="stringliteral">&quot;Epoch &quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>() &lt;&lt; <span class="stringliteral">&quot;: baseline loss: &quot;</span> &lt;&lt; baseline_loss &lt;&lt; endl;
<a name="l00329"></a>00329   cerr &lt;&lt; <span class="stringliteral">&quot;Epoch &quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>() &lt;&lt; <span class="stringliteral">&quot;: average devtest loss: &quot;</span>
<a name="l00330"></a>00330        &lt;&lt; loss_this_epoch &lt;&lt; endl;
<a name="l00331"></a>00331   cerr &lt;&lt; <span class="stringliteral">&quot;Epoch &quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>() &lt;&lt; <span class="stringliteral">&quot;: number of testing errors: &quot;</span>
<a name="l00332"></a>00332        &lt;&lt; num_testing_errors_this_epoch &lt;&lt; <span class="stringliteral">&quot; (&quot;</span>
<a name="l00333"></a>00333        &lt;&lt; percent_testing_errors_this_epoch &lt;&lt; <span class="stringliteral">&quot;%)&quot;</span> &lt;&lt; endl;
<a name="l00334"></a>00334 
<a name="l00335"></a>00335   <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>() == 0 ||
<a name="l00336"></a>00336       loss_this_epoch &lt; <a class="code" href="classreranker_1_1_model.html#ad3a296860f768bee866213cc0c8bbf83" title="The average loss per epoch.">loss_per_epoch_</a>[<a class="code" href="classreranker_1_1_perceptron_model.html#acc08b37db0b4b7ea4cd8d340a4e86216" title="The epoch of the best models seen so far during training.">best_model_epoch_</a>]) {
<a name="l00337"></a>00337     <a class="code" href="classreranker_1_1_perceptron_model.html#a234283287d5a7f59fd8a1d1cf767fbc1" title="The best models seen so far during training, according to evaluation on the held-out development test...">best_models_</a> = <a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>;
<a name="l00338"></a>00338     <a class="code" href="classreranker_1_1_perceptron_model.html#acc08b37db0b4b7ea4cd8d340a4e86216" title="The epoch of the best models seen so far during training.">best_model_epoch_</a> = <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>();
<a name="l00339"></a>00339   }
<a name="l00340"></a>00340 
<a name="l00341"></a>00341   <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>() &gt; 0 &amp;&amp;
<a name="l00342"></a>00342       <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a18d952cada06cc20fb8382b098e1ee98" title="Returns the index of the current epoch.">epoch</a>() != <a class="code" href="classreranker_1_1_perceptron_model.html#acc08b37db0b4b7ea4cd8d340a4e86216" title="The epoch of the best models seen so far during training.">best_model_epoch_</a> &amp;&amp;
<a name="l00343"></a>00343       loss_this_epoch &gt;= <a class="code" href="classreranker_1_1_model.html#ad3a296860f768bee866213cc0c8bbf83" title="The average loss per epoch.">loss_per_epoch_</a>[<a class="code" href="classreranker_1_1_perceptron_model.html#acc08b37db0b4b7ea4cd8d340a4e86216" title="The epoch of the best models seen so far during training.">best_model_epoch_</a>]) {
<a name="l00344"></a>00344     ++<a class="code" href="classreranker_1_1_perceptron_model.html#a0c25e67c7e7e5cef36bbc1b1fb1cd111" title="The current number of training epochs in which the model has been degrading in development set perfor...">num_epochs_in_decline_</a>;
<a name="l00345"></a>00345   } <span class="keywordflow">else</span> {
<a name="l00346"></a>00346     <span class="comment">// We&#39;re in the first epoch, or we&#39;ve made strictly fewer errors</span>
<a name="l00347"></a>00347     <span class="comment">// than the previous epoch.</span>
<a name="l00348"></a>00348     <a class="code" href="classreranker_1_1_perceptron_model.html#a0c25e67c7e7e5cef36bbc1b1fb1cd111" title="The current number of training epochs in which the model has been degrading in development set perfor...">num_epochs_in_decline_</a> = 0;
<a name="l00349"></a>00349   }
<a name="l00350"></a>00350   <span class="keywordflow">return</span> loss_this_epoch;
<a name="l00351"></a>00351 }
<a name="l00352"></a>00352 
<a name="l00353"></a>00353 <span class="keywordtype">void</span>
<a name="l00354"></a><a class="code" href="classreranker_1_1_perceptron_model.html#ace55bad139573080e682d5a269760673">00354</a> <a class="code" href="classreranker_1_1_perceptron_model.html#ace55bad139573080e682d5a269760673" title="Scores the specified set of candidates according to either the raw or averaged version of this percep...">PerceptronModel::ScoreCandidates</a>(<a class="code" href="classreranker_1_1_candidate_set.html" title="A class to hold a set of candidates, either for training or test.">CandidateSet</a> &amp;candidates, <span class="keywordtype">bool</span> training) {
<a name="l00355"></a>00355   <a class="code" href="classreranker_1_1_model.html#abf7147622fb3c5801016fb7c479f3158" title="A scorer for CandidateSet instances.">candidate_set_scorer_</a>-&gt;Score(<span class="keyword">this</span>, candidates, training);
<a name="l00356"></a>00356 }
<a name="l00357"></a>00357 
<a name="l00358"></a>00358 <span class="keywordtype">double</span>
<a name="l00359"></a><a class="code" href="classreranker_1_1_perceptron_model.html#aff26021ce19337a90068b66a219c0c6c">00359</a> <a class="code" href="classreranker_1_1_perceptron_model.html#aff26021ce19337a90068b66a219c0c6c" title="Scores a candidate according to either the raw or averaged version of this perceptron model...">PerceptronModel::ScoreCandidate</a>(<a class="code" href="classreranker_1_1_candidate.html" title="A class to represent a candidate in a set of candidates that constitutes a training instance for a re...">Candidate</a> &amp;candidate, <span class="keywordtype">bool</span> training) {
<a name="l00360"></a>00360   <span class="keywordtype">bool</span> use_raw = training;
<a name="l00361"></a>00361   <span class="keyword">const</span> <a class="code" href="classreranker_1_1_feature_vector.html">FeatureVector&lt;int,double&gt;</a> &amp;model = <a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#a98dd763d1e1b3b980e07091488f9b422" title="Returns either the raw or averaged feature vector, depending on the argument.">GetModel</a>(use_raw);
<a name="l00362"></a>00362   <span class="keywordtype">double</span> score = <a class="code" href="classreranker_1_1_model.html#a50128261606a9cb655493b3f9b9149c4" title="Yes, this is an interface, but we add the kernel function as a data member.">kernel_fn_</a>-&gt;<a class="code" href="classreranker_1_1_kernel_function.html#ab274007dc0adedfaeb835910ccb5435a" title="Applies this kernel function to the specified feature vectors.">Apply</a>(model, candidate.<a class="code" href="classreranker_1_1_candidate.html#a741433c66a4cc4dfbb1e4b89c9c6e431" title="Returns the feature vector for this candidate.">features</a>());
<a name="l00363"></a>00363   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a> &gt;= 2) {
<a name="l00364"></a>00364     cerr &lt;&lt; <span class="stringliteral">&quot;Time:&quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a1e8b9be0b48cd8cbb360f23a638e12c6">to_string</a>() &lt;&lt; <span class="stringliteral">&quot;: scoring candidate &quot;</span>
<a name="l00365"></a>00365          &lt;&lt; candidate &lt;&lt; <span class="stringliteral">&quot; with &quot;</span> &lt;&lt; (use_raw ? <span class="stringliteral">&quot;raw&quot;</span> : <span class="stringliteral">&quot;avg&quot;</span>)
<a name="l00366"></a>00366          &lt;&lt; <span class="stringliteral">&quot; model: &quot;</span> &lt;&lt; model &lt;&lt; endl
<a name="l00367"></a>00367          &lt;&lt; <span class="stringliteral">&quot;\tscore: &quot;</span> &lt;&lt; score &lt;&lt; endl;
<a name="l00368"></a>00368   }
<a name="l00369"></a>00369   candidate.<a class="code" href="classreranker_1_1_candidate.html#a1716a1e38f3b681a60aa5eaedbdc5f0b" title="Sets the score of this candidate.">set_score</a>(score);
<a name="l00370"></a>00370   <span class="keywordflow">return</span> score;
<a name="l00371"></a>00371 }
<a name="l00372"></a>00372 
<a name="l00373"></a>00373 <span class="keywordtype">void</span>
<a name="l00374"></a><a class="code" href="classreranker_1_1_perceptron_model.html#a919a86a6a3a52f62199b438fb092836d">00374</a> <a class="code" href="classreranker_1_1_perceptron_model.html#a919a86a6a3a52f62199b438fb092836d">PerceptronModel::CompactifyFeatureUids</a>() {
<a name="l00375"></a>00375   <span class="comment">// First, produce mapping for uid&#39;s of current non-zero features to dense</span>
<a name="l00376"></a>00376   <span class="comment">// interval [0,n-1] (where there are n non-zero features).</span>
<a name="l00377"></a>00377   unordered_set&lt;int&gt; old_uids;
<a name="l00378"></a>00378   <a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#a49b3808a509cf2e46edfdc4df6fbd62d" title="Returns the &quot;raw&quot; feature weights computed during training.">weights</a>().<a class="code" href="classreranker_1_1_feature_vector.html#a87bccd3acb055b98a674e6ed7d29af4b" title="Inserts the uid&#39;s of features with non-zero weights into the specified set.">GetNonZeroFeatures</a>(old_uids);
<a name="l00379"></a>00379   <a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#a21e449676837c2a0aa03a6749bc9b6f7" title="Returns the feature vector corresponding to the averaged perceptron.">average_weights</a>().<a class="code" href="classreranker_1_1_feature_vector.html#a87bccd3acb055b98a674e6ed7d29af4b" title="Inserts the uid&#39;s of features with non-zero weights into the specified set.">GetNonZeroFeatures</a>(old_uids);
<a name="l00380"></a>00380   unordered_map&lt;int, int&gt; old_to_new_uids;
<a name="l00381"></a>00381   <span class="keywordtype">int</span> new_uid = 0;
<a name="l00382"></a>00382   <span class="keywordflow">for</span> (unordered_set&lt;int&gt;::const_iterator it = old_uids.begin();
<a name="l00383"></a>00383        it != old_uids.end();
<a name="l00384"></a>00384        ++it) {
<a name="l00385"></a>00385     old_to_new_uids[*it] = new_uid++;
<a name="l00386"></a>00386   }
<a name="l00387"></a>00387   <a class="code" href="classreranker_1_1_perceptron_model.html#a6f913688d02fe624d91d1416c71e157c" title="The feature vectors representing this model.">models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#acd8ae561c7437253df45616432d3a683">RemapFeatureUids</a>(old_to_new_uids);
<a name="l00388"></a>00388   <a class="code" href="classreranker_1_1_perceptron_model.html#a234283287d5a7f59fd8a1d1cf767fbc1" title="The best models seen so far during training, according to evaluation on the held-out development test...">best_models_</a>.<a class="code" href="classreranker_1_1_training_vector_set.html#acd8ae561c7437253df45616432d3a683">RemapFeatureUids</a>(old_to_new_uids);
<a name="l00389"></a>00389 
<a name="l00390"></a>00390   <span class="keywordflow">if</span> (<a class="code" href="classreranker_1_1_model.html#a8c0f904234605692035ba179f85c53e2" title="The symbol table for this model (may be NULL).">symbols_</a> != NULL) {
<a name="l00391"></a>00391     <a class="code" href="classreranker_1_1_symbols.html" title="An interface specifying a converter from symbols (strings) to int indices.">Symbols</a> *old_symbols = <a class="code" href="classreranker_1_1_model.html#a8c0f904234605692035ba179f85c53e2" title="The symbol table for this model (may be NULL).">symbols_</a>-&gt;<a class="code" href="classreranker_1_1_symbols.html#aaeac8d6c3a9d088aa4f086fa1cf6b2fa" title="Creates a newly-constructed clone of this Symbols instance that has the same runtime type...">Clone</a>();
<a name="l00392"></a>00392     <a class="code" href="classreranker_1_1_model.html#a8c0f904234605692035ba179f85c53e2" title="The symbol table for this model (may be NULL).">symbols_</a>-&gt;<a class="code" href="classreranker_1_1_symbols.html#a3c78e8ddc4a1a1375968a214f4bdda3e" title="Clears all symbols from this symbol table.">Clear</a>();
<a name="l00393"></a>00393     <span class="keywordflow">for</span> (<a class="code" href="classreranker_1_1_symbols.html#ac2f04202711c3f61038ca43656edeb92">Symbols::const_iterator</a> it = old_symbols-&gt;<a class="code" href="classreranker_1_1_symbols.html#ae63ad8cbeb0ab390a50e9f8b8deaa998">begin</a>();
<a name="l00394"></a>00394          it != old_symbols-&gt;<a class="code" href="classreranker_1_1_symbols.html#a299004b5ead84217cc11ce43dd41b359">end</a>();
<a name="l00395"></a>00395          ++it) {
<a name="l00396"></a>00396       unordered_map&lt;int, int&gt;::const_iterator old_to_new_uid_it =
<a name="l00397"></a>00397           old_to_new_uids.find(it-&gt;second);
<a name="l00398"></a>00398       <span class="keywordflow">if</span> (old_to_new_uid_it != old_to_new_uids.end()) {
<a name="l00399"></a>00399         <span class="keywordtype">int</span> new_uid = old_to_new_uid_it-&gt;second;
<a name="l00400"></a>00400         <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;symbol = it-&gt;first;
<a name="l00401"></a>00401         <a class="code" href="classreranker_1_1_model.html#a8c0f904234605692035ba179f85c53e2" title="The symbol table for this model (may be NULL).">symbols_</a>-&gt;<a class="code" href="classreranker_1_1_symbols.html#aae3fb0d876b17204049c3de8ae8b30f9">SetIndex</a>(symbol, new_uid);
<a name="l00402"></a>00402       }
<a name="l00403"></a>00403     }
<a name="l00404"></a>00404     <span class="keyword">delete</span> old_symbols;
<a name="l00405"></a>00405   }
<a name="l00406"></a>00406 }
<a name="l00407"></a>00407 
<a name="l00408"></a>00408 <span class="keywordtype">void</span>
<a name="l00409"></a><a class="code" href="classreranker_1_1_perceptron_model.html#af7b4bb2f10d16bcc6bdf2428e6c09af3">00409</a> <a class="code" href="classreranker_1_1_perceptron_model.html#af7b4bb2f10d16bcc6bdf2428e6c09af3" title="Computes the features to be updated for the gold candidate and the best-scoring candidate.">PerceptronModel::ComputeFeaturesToUpdate</a>(<span class="keyword">const</span> <a class="code" href="classreranker_1_1_candidate_set.html" title="A class to hold a set of candidates, either for training or test.">CandidateSet</a> &amp;example,
<a name="l00410"></a>00410                                          unordered_set&lt;int&gt; &amp;
<a name="l00411"></a>00411                                          gold_features_to_update,
<a name="l00412"></a>00412                                          unordered_set&lt;int&gt; &amp;
<a name="l00413"></a>00413                                          best_scoring_features_to_update)<span class="keyword"></span>
<a name="l00414"></a>00414 <span class="keyword">    const </span>{
<a name="l00415"></a>00415   <span class="comment">// Collect gold features that are not in best-scoring candidate.</span>
<a name="l00416"></a>00416   <span class="keyword">const</span> <a class="code" href="classreranker_1_1_feature_vector.html">FeatureVector&lt;int,double&gt;</a> &amp;gold_features =
<a name="l00417"></a>00417       example.<a class="code" href="classreranker_1_1_candidate_set.html#a30da3565fca2f14edafafce0255a35ea">GetGold</a>().<a class="code" href="classreranker_1_1_candidate.html#a741433c66a4cc4dfbb1e4b89c9c6e431" title="Returns the feature vector for this candidate.">features</a>();
<a name="l00418"></a>00418   <span class="keyword">const</span> <a class="code" href="classreranker_1_1_feature_vector.html">FeatureVector&lt;int,double&gt;</a> &amp;best_scoring_features =
<a name="l00419"></a>00419       example.<a class="code" href="classreranker_1_1_candidate_set.html#ae2d8533d6d2d3cd10a2c23a8660eefc3">GetBestScoring</a>().<a class="code" href="classreranker_1_1_candidate.html#a741433c66a4cc4dfbb1e4b89c9c6e431" title="Returns the feature vector for this candidate.">features</a>();
<a name="l00420"></a>00420 
<a name="l00421"></a>00421   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a> &gt;= 2) {
<a name="l00422"></a>00422     cerr &lt;&lt; <span class="stringliteral">&quot;Gold index: &quot;</span> &lt;&lt; example.<a class="code" href="classreranker_1_1_candidate_set.html#aec1af01230233ff19c30174a93a1e1b4">gold_index</a>()
<a name="l00423"></a>00423          &lt;&lt; <span class="stringliteral">&quot;; best scoring index: &quot;</span> &lt;&lt; example.<a class="code" href="classreranker_1_1_candidate_set.html#a6052873186168c21186705c3e016d223">best_scoring_index</a>()
<a name="l00424"></a>00424          &lt;&lt; endl;
<a name="l00425"></a>00425     cerr &lt;&lt; <span class="stringliteral">&quot;Original gold features: &quot;</span> &lt;&lt; gold_features &lt;&lt; endl
<a name="l00426"></a>00426          &lt;&lt; <span class="stringliteral">&quot;Original best scoring features: &quot;</span> &lt;&lt; best_scoring_features &lt;&lt; endl;
<a name="l00427"></a>00427   }
<a name="l00428"></a>00428 
<a name="l00429"></a>00429   gold_features.<a class="code" href="classreranker_1_1_feature_vector.html#a87bccd3acb055b98a674e6ed7d29af4b" title="Inserts the uid&#39;s of features with non-zero weights into the specified set.">GetNonZeroFeatures</a>(gold_features_to_update);
<a name="l00430"></a>00430   best_scoring_features.<a class="code" href="classreranker_1_1_feature_vector.html#a56cdaf41599879784f9342e6d94250b0" title="Removes from the specified set the uid&#39;s of feature with weights equal in this vector to their weight...">RemoveEqualFeatures</a>(gold_features,
<a name="l00431"></a>00431                                             gold_features_to_update);
<a name="l00432"></a>00432 
<a name="l00433"></a>00433   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a> &gt;= 2) {
<a name="l00434"></a>00434     cerr &lt;&lt; <span class="stringliteral">&quot;Time:&quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a1e8b9be0b48cd8cbb360f23a638e12c6">to_string</a>() &lt;&lt; <span class="stringliteral">&quot;: new gold features: [&quot;</span>;
<a name="l00435"></a>00435     <span class="keywordflow">for</span> (unordered_set&lt;int&gt;::const_iterator it =
<a name="l00436"></a>00436              gold_features_to_update.begin();
<a name="l00437"></a>00437          it != gold_features_to_update.end();
<a name="l00438"></a>00438          ++it) {
<a name="l00439"></a>00439       cerr &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; *it;
<a name="l00440"></a>00440     }
<a name="l00441"></a>00441     cerr &lt;&lt; <span class="stringliteral">&quot;]&quot;</span> &lt;&lt; endl;
<a name="l00442"></a>00442   }
<a name="l00443"></a>00443 
<a name="l00444"></a>00444   best_scoring_features.<a class="code" href="classreranker_1_1_feature_vector.html#a87bccd3acb055b98a674e6ed7d29af4b" title="Inserts the uid&#39;s of features with non-zero weights into the specified set.">GetNonZeroFeatures</a>(best_scoring_features_to_update);
<a name="l00445"></a>00445   gold_features.<a class="code" href="classreranker_1_1_feature_vector.html#a56cdaf41599879784f9342e6d94250b0" title="Removes from the specified set the uid&#39;s of feature with weights equal in this vector to their weight...">RemoveEqualFeatures</a>(best_scoring_features,
<a name="l00446"></a>00446                                     best_scoring_features_to_update);
<a name="l00447"></a>00447   <span class="keywordflow">if</span> (<a class="code" href="perceptron-model_8_c.html#ad72dbcf6d0153db1b8d8a58001feed83">DEBUG</a> &gt;= 2) {
<a name="l00448"></a>00448     cerr &lt;&lt; <span class="stringliteral">&quot;Time:&quot;</span> &lt;&lt; <a class="code" href="classreranker_1_1_model.html#ac23eff7aa52c83c566658bcba9093c4f" title="The tiny object that holds the &quot;training time&quot; for this model (epoch, index and absolute time index)...">time_</a>.<a class="code" href="classreranker_1_1_time.html#a1e8b9be0b48cd8cbb360f23a638e12c6">to_string</a>() &lt;&lt; <span class="stringliteral">&quot;: new best scoring features: [&quot;</span>;
<a name="l00449"></a>00449     <span class="keywordflow">for</span> (unordered_set&lt;int&gt;::const_iterator it =
<a name="l00450"></a>00450              best_scoring_features_to_update.begin();
<a name="l00451"></a>00451          it != best_scoring_features_to_update.end();
<a name="l00452"></a>00452          ++it) {
<a name="l00453"></a>00453       cerr &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; *it;
<a name="l00454"></a>00454     }
<a name="l00455"></a>00455     cerr &lt;&lt; <span class="stringliteral">&quot;]&quot;</span> &lt;&lt; endl;
<a name="l00456"></a>00456   }
<a name="l00457"></a>00457 
<a name="l00458"></a>00458 }
<a name="l00459"></a>00459 
<a name="l00460"></a>00460 }  <span class="comment">// namespace reranker</span>
</pre></div></div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated on Tue Jan 7 2014 15:24:58 for Reranker Framework (ReFr) by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
